package com.gz.spring.tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class GoodsDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    public Float queryPriceById(Integer id) {

        String sql = "SELECT price From goods Where goods_id=?";
        Float price = jdbcTemplate.queryForObject(sql, Float.class, id);
        return price;
    }

    public void updateBalance(Integer user_id, Float money) {

        String sql = "UPDATE user_account SET money=money-? Where user_id=?";
        jdbcTemplate.update(sql, money, user_id);
    }

    public void updateAmount(Integer goods_id,Integer amount) {
        String sql="update goods_amount set goods_num=goods_num-? Where goods_id=?";
        jdbcTemplate.update(sql, amount, goods_id);
    }

    public Float queryPriceById2(Integer id) {

        String sql = "SELECT price From goods Where goods_id=?";
        Float price = jdbcTemplate.queryForObject(sql, Float.class, id);
        return price;
    }

    public void updateBalance2(Integer user_id, Float money) {

        String sql = "UPDATE user_account SET money=money-? Where user_id=?";
        jdbcTemplate.update(sql, money, user_id);
    }

    public void updateAmount2(Integer goods_id,Integer amount) {
        String sql="update goods_amount set goods_num=goods_num-? Where goods_id=?";
        jdbcTemplate.update(sql, amount, goods_id);
    }
}
